package org.japo.java.basics.entities;

public class Fecha {

    public final static String[] MESES = {"Enero", "Febrero", "Marzo", "Abril", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"};
    public final static String[] DIAS = {"Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado", "Domingo"};
    public final static String[] ESTACIONES = {"Primavera", "Verano", "Otoño", "Invierno"};
    private int dia;
    private int mes;
    private int any;

    public Fecha() {
        dia = 1;
        mes = 1;
        any = 0;
    }

    public Fecha(int dia, int mes, int any) {
        if (validarFecha(dia, mes, any)) {
            this.dia = dia;
            this.mes = mes;
            this.any = any;
        } else {
            this.dia = 1;
            this.mes = 1;
        }
    }

    public static boolean validarFecha(int dia, int mes, int any) {

        return mes <= 12 || mes >= 1 || dia >= 1 || ((any % 400 == 0 || any % 100 != 0 && any % 4 == 0) && (mes == 2 && dia <= 29)) || ((any % 400 != 0 || any % 100 == 0 && any % 4 == 0 || any % 4 != 0) && (mes == 2 && dia <= 28)) || ((mes == 1 || mes == 3 || mes == 5 || mes == 7 || mes == 10 || mes == 12) && (dia <= 31)) || ((mes == 4 || mes == 6 || mes == 8 || mes == 9 || mes == 11) && (dia <= 30));
    }

    public String toString() {
        return String.format("%02d/%02d/%d", dia, mes, any);
    }

    public void mostrarFechaCarta() {
        System.out.println(toString());
    }

    public void mostrarFechaLarga() {
        System.out.printf(String.format("%d de %s de %d", dia, MESES[mes - 1], any));
    }

    @Override
    public boolean equals(Object obj) {
        boolean testOK;
        if (obj == null)
            testOK = false;
        else if (!(obj instanceof Fecha)) {
            testOK = false;
        } else {
            Fecha f = (Fecha) obj;
            testOK = f.dia == dia && f.mes == mes && f.any == any;
        }
        return testOK;
    }

}
